package com.qf.business.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.business.system.mapper.RoleMapper;
import com.qf.commons.redis.cache.annotation.CacheDel;
import com.qf.commons.redis.cache.annotation.CacheGet;
import com.qf.data.system.dto.RoleDto;
import com.qf.data.system.dto.RoleDto2;
import com.qf.data.system.entity.Role;
import com.qf.business.system.service.RoleService;
import com.qf.data.system.entity.RolePowerTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色表(Role)表服务实现类
 *
 * @author makejava
 * @since 2022-05-26 16:17:12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePowerTableServiceImpl rolePowerTableService;

    @Override
    @CacheDel("'role_list'")
    public boolean save(Role entity) {
        return super.save(entity);
    }

    @Override
    @CacheGet("'role_list'")
    public List<RoleDto> queryList() {
        return roleMapper.queryList();
    }

    /**
     * 根据角色id，修改对应的权限
     * @param rid
     * @param pids
     * @return
     */
    @Override
    public int updatePowersByRid(Long rid, List<Long> pids) {
        //根据rid,删除角色权限中间表
        rolePowerTableService.removeById(rid);

        //根据角色id和权限id插入中间表  -批量插入
        List<RolePowerTable> rolePowerTables = new ArrayList<>();
        for (Long pid : pids) {
            RolePowerTable rolePowerTable = new RolePowerTable()
                    .setRid(rid)
                    .setPid(pid);
            rolePowerTables.add(rolePowerTable);
        }
        rolePowerTableService.saveBatch(rolePowerTables);
        return 1;
    }

    @Override
    public List<RoleDto2> queryRoleByDepAndEmp(Long depId, Long empId) {
        return roleMapper.queryRoleByDepAndEmp(depId,empId);
    }


}


